import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/myhome",
    name: "myhome",
    component: () => import("../views/home/MyHome.vue"),
    children: [
      {
        path: "userlist",
        name: "userlist",
        component: () => import("../views/home/UserList.vue"),
      },
      {
        path: "updatauser",
        name: "updatauser",
        component: () => import("../views/home/UpdataUser.vue"),
      },
      {
        path: "mydata",
        name: "mydata",
        component: () => import("../views/home/MyData.vue"),
      },
      {
        path: "myconsume",
        name: "myconsume",
        component: () => import("../views/home/MyConsume.vue"),
      },
      {
        path: "myshowing",
        name: "myshowing",
        component: () => import("../views/home/MyShowing.vue"),
      },
      {
        path: "idetail",
        name: "idetail",
        component: () => import("../components/IntendantDetail.vue"),
      },
      {
        path: "revamp",
        name: "revamp",
        component: () => import("../components/RevampPass.vue"),
      },
    ]
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/ShouRouter.vue")
  },


]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path !== "/login") {
    if (!localStorage.getItem("token")) {
      router.push("/login")
    } else {
      next()
    }
  } else {
    next()
  }
})
router.afterEach((to, from) => {


  switch (to.path) {
    case "/myhome/mydata":
      localStorage.setItem("active", "1");
      break;
    case "/myhome/myconsume":
      localStorage.setItem("active", "2");
      break;
    case "/myhome/userlist":
      localStorage.setItem("active", "3");
      break;
    case "/myhome/myshowing":
      localStorage.setItem("active", "4");
      break;
    default:
      localStorage.setItem("active", "1");
      break;
  }

})


export default router
